package gui.components;

import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;

import game.PlayerDAO;
import gui.game.GUI;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JLabel;
import javax.swing.JPanel;

@SuppressWarnings("serial")
public class PlayerPanel extends JPanel{
	private PlayerDAO player;
	private JLabel name;
	private JLabel money;
	private JLabel moneyDesc;
	private JLabel color;
	private int SIZE;
	private JLabel border;
	private JLabel figure;
	private JLabel figureDesc;
	private static int instances;
	
	
	public PlayerPanel(PlayerDAO inPlayer){
		instances++;
		player = inPlayer;
		figure = new JLabel();
		figure.setIcon(player.getIcon());
		SIZE = GUI.getSIZE();
		name = new JLabel();
		name.setText(player.getName());
		name.setFont(new Font ("Arial", Font.PLAIN, 30/SIZE));
		money = new JLabel();
		money.setText(formateMoney(((Integer)(player.getMoney()))));
		money.setFont(new Font ("Arial", Font.PLAIN, 30/SIZE));
		color = new JLabel();
		color.setOpaque(true);
		color.setBackground(player.getColor());
		border = new JLabel();
		figureDesc = new JLabel();
		figureDesc.setText(player.getName()+" figure:");
		figureDesc.setFont(new Font ("Arial", Font.PLAIN, 12/SIZE));
		moneyDesc = new JLabel();
		moneyDesc.setText(player.getName()+" money:");
		moneyDesc.setFont(new Font ("Arial", Font.PLAIN, 12/SIZE));
		
		
		this.setLayout(null);
//		this.setBackground(player.getColor());
		//color.setBorder(BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.black), "Color"));
		color.setBorder(BorderFactory.createLineBorder(Color.black));
		color.setBounds(25/SIZE, 25, 50/SIZE, 40/SIZE);
		name.setBounds(80/SIZE, 25, 150/SIZE, 50/SIZE);
		moneyDesc.setBounds(25/SIZE, 105/SIZE, moneyDesc.getPreferredSize().width, moneyDesc.getPreferredSize().height);
		money.setBounds(moneyDesc.getPreferredSize().width/SIZE+50/SIZE, 85/SIZE, 250/SIZE, 50/SIZE);
		border.setBorder(BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.black), "Player "+instances+" Info"));
		border.setBounds(0,0,320/SIZE,200/SIZE);
		figureDesc.setBounds(25/SIZE,150/SIZE,figureDesc.getPreferredSize().width, figureDesc.getPreferredSize().height);
		figure.setBounds(figureDesc.getPreferredSize().width/SIZE+50/SIZE, 130/SIZE, figure.getPreferredSize().width, figure.getPreferredSize().height);
		
		
		this.add(figureDesc);
		this.add(figure);
		this.add(color);
		this.add(name);
		this.add(moneyDesc);
		this.add(money);
		this.add(border);
		
	}
	
	private String formateMoney(Integer money){
		String temp = money.toString();
		int front = temp.length()%3;
		String result = temp.substring(0,front) + ".";
		temp = temp.substring(front);
		while (temp.length()>3){
			result += temp.substring(0,3) + ".";
			temp = temp.substring(3);
		}
		result += temp + "$";
		return result;
		
	}
	
	public void repaintPanel(){
		money.setText(formateMoney(((Integer)(player.getMoney()))));
		name.setText(player.getName());
		this.repaint();		
	}
	
	
}
